package main

import (
	"context"
	"google.golang.org/grpc"
	pb "grpc-demo/complex/proto"
	"io"
	"log"
	"strconv"
)

var grpcClient pb.StreamClient

func conversation() {
	stream, err := grpcClient.Conversation(context.Background())
	if err != nil {
		log.Fatal(err)
	}
	for i := 0; i < 5; i++ {
		err = stream.Send(&pb.StreamRequest{
			StreamData: "bi-directory stream client " + strconv.Itoa(i)})
		if err != nil {
			log.Fatal(err)
		}
		res, err := stream.Recv()
		if err == io.EOF {
			break
		}
		if err != nil {
			log.Fatal(err)
		}
		log.Println(res.StreamValue)
	}
	err = stream.CloseSend()
	if err != nil {
		log.Fatal(err)
	}
}

func main() {
	conn, err := grpc.Dial(":50051", grpc.WithInsecure())
	if err != nil {
		log.Fatal(err)
	}
	defer conn.Close()

	grpcClient = pb.NewStreamClient(conn)

	conversation()
}
